package web;
import service.EnquireService;
import service.PageService;
import pojo.PageBean;
import pojo.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author fire'ge
 */
@WebServlet("/checkServlet")
public class CheckServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //用户点击页码，从前端传输start和rows两个数据，在数据库进行分页查询

        int start = 0;
        String startStr = request.getParameter("start");
        if ( startStr!= null){
            start = Integer.parseInt(startStr);
        }
        int pageRows = 9;
        String factorStr = request.getParameter("factor");
        PageBean pageBean = null;
        try {
            //获得pageBean对象，里面有两个属性，总页码和展示的数据
            if (factorStr == null){
                pageBean = PageService.getPageList(start,pageRows);
            }
            else {
                pageBean = EnquireService.getChecklist(start,pageRows,factorStr);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        List<User> users = null;
        if (pageBean != null) {
            users = pageBean.getList();
        }
        Integer page = null;
        if (pageBean != null) {
            page = pageBean.getPage();
        }
        //将数据传入到前端，使前端拿到一页的数据和总数据进行展示
        request.setAttribute("factor2",factorStr);
        request.setAttribute("users",users);
        request.setAttribute("rows",page);
        request.getRequestDispatcher("/jsp/jstl2.jsp").forward(request,response);



    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
